package com.example.main

import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.RoutePath
import com.example.common.base.BaseActivity
import com.example.main.adapter.MainPageAdapter
import com.example.main.databinding.ActivityMainNavBinding
import com.example.main.tab.NormalTabView
import com.example.main.tab.RoundTabView
import dagger.hilt.android.AndroidEntryPoint
import me.majiajie.pagerbottomtabstrip.NavigationController
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener


@Route(path = RoutePath.ROUTE_MAIN_NAV)
@AndroidEntryPoint
class MainNavActivity : BaseActivity<ActivityMainNavBinding>() {

    private var fragments = arrayListOf<Fragment>()
    private lateinit var mPageAdapter: MainPageAdapter
    private lateinit var mNavController: NavigationController

    override fun getLayoutId(): Int = R.layout.activity_main_nav

    override fun initViews() {
        //初始化底部导航组件
        mNavController = mDataBinding.mainBottomNav.custom()
            .addItem(newTabItem(R.drawable.nav_home_normal, R.drawable.nav_home_checked, "首页")) //首页
            .addItem(newTabItem(R.drawable.nav_movie_normal, R.drawable.nav_movie_checked, "放映厅")) //放映厅
            .addItem(newRoundTabItem(R.drawable.nav_add_normal, R.drawable.nav_add_checked, "发布")) //发布
            .addItem(newTabItem(R.drawable.nav_live_normal, R.drawable.nav_live_checked, "直播")) //直播
            .addItem(newTabItem(R.drawable.nav_mine_normal, R.drawable.nav_mine_checked, "我的")) //我的
            .build()

        //初始化Fragment数据
        fragments.add(ARouter.getInstance().build(RoutePath.ROUTE_HOME).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RoutePath.ROUTE_MOVIE).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RoutePath.ROUTE_LIVE).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RoutePath.ROUTE_MINE).navigation() as Fragment)

        //设置页面数据适配器
        mPageAdapter = MainPageAdapter(supportFragmentManager, fragments, FragmentPagerAdapter.BEHAVIOR_SET_USER_VISIBLE_HINT)
        mDataBinding.mainViewPager.adapter = mPageAdapter
        mDataBinding.mainViewPager.offscreenPageLimit = 3 //设置ViewPager前后缓存页面个数

        //设置tab的选中监听事件
        mNavController.addTabItemSelectedListener(object : OnTabItemSelectedListener {

            override fun onSelected(index: Int, old: Int) {
                when(index){
                    0, 1 -> mDataBinding.mainViewPager.currentItem = index
                    2 -> {
                        ARouter.getInstance().build(RoutePath.ROUTE_PUBLISH_PAGE).navigation()
                        mNavController.setSelect(old)
                    }
                    3, 4 -> mDataBinding.mainViewPager.currentItem = index - 1
                }
            }

            override fun onRepeat(index: Int) {
                Log.i("======", "onRepeat: Index：$index")
                if (index == 2) {
                    ARouter.getInstance().build(RoutePath.ROUTE_PUBLISH_PAGE).navigation()
                }
            }
        })

        //设置ViewPager页面监听事件
        mDataBinding.mainViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                Log.i("======", "onPageScrolled: position：$position")
            }

            override fun onPageSelected(position: Int) {
                Log.i("======", "onPageSelected: position：$position")
                when(position){
                    0, 1 ->  mNavController.setSelect(position)
                    2, 3 ->  mNavController.setSelect(position + 1)
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
                Log.i("======", "onPageScrollStateChanged: state：$state")
            }
        })

    }

    private fun newRoundTabItem(defaultResId: Int, checkedResId: Int, title: String): BaseTabItem{
        var roundTabView = RoundTabView(this@MainNavActivity).apply {
            initData(defaultResId, checkedResId, title)
            setDefaultTextColor(R.color.main_bottom_default_color)
            setCheckedTextColor(R.color.main_bottom_check_color)
        }
        return  roundTabView
    }


    private fun newTabItem(defaultResId: Int, checkedResId: Int, title: String, hasMsg: Boolean = false, msgNum: Int = 0): BaseTabItem{
        var norTabView = NormalTabView(this@MainNavActivity).apply {
            initData(defaultResId, checkedResId, title)
            setDefaultTextColor(R.color.main_bottom_default_color)
            setCheckedTextColor(R.color.main_bottom_check_color)
            if(hasMsg && msgNum > 0){
                setHasMessage(true)
                setMessageNumber(msgNum)
            }
        }
        return  norTabView
    }

}